function wxpay(app, orderId, redirectUrl) {
  wx.request({
    url: app.globalData.host + '/api/wechat/pay',
    data: {
      uid: app.globalData.userInfo.Uid,
      storeId: app.globalData.storeId,
      oid: orderId
    },
    success: function (res) {
      console.log('api result:');
      console.log(res);
      if (res.data.ExceptionCode == 302){
        wx.redirectTo({
          url: redirectUrl
        });
        return;
      }
      if (res.data.Code == 0) {
        // 发起支付
        wx.requestPayment({
          timeStamp:res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          fail: function (aaa) {
            console.log(aaa)
            wx.redirectTo({
              url: redirectUrl
            });
            return;
          },
          success: function () {
            wx.showToast({ title: '支付成功' })
            wx.redirectTo({
              url: redirectUrl
            });
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: redirectUrl
              });
            } 
          }
        })
        return;
      }
    }
  })
}
// 现金充值
function wxcashpay(app, orderId, redirectUrl) {
  wx.request({
    url: app.globalData.host + '/api/WeChat/CashPay',
    data: {
      storeId: app.globalData.storeId,
      uid: app.globalData.userInfo.Uid,
      oid: orderId
    },
    success: function (res) {
      if(res.data.Code == 0){
        //发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          success: function (res) {
            wx.showToast({ title: '支付成功'})
            wx.redirectTo({
              url: redirectUrl
            });
          },
          fail: function (aaa) {
            wx.showModal({
              title: '提示',
              content: aaa.errMsg,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: redirectUrl
                  });
                }
              }
            })
            return;
          }
        })
      }
      else{
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: redirectUrl
              });
            }
          }
        })
        return;
      }
    }
  })
}
// 当面付款
function wxdirectpay(app, orderId, redirectUrl) {
  console.log(orderId)
  wx.request({
    url: app.globalData.host + '/api/WeChat/DirectPay',
    data: {
      storeId: app.globalData.storeId,
      uid: app.globalData.userInfo.Uid,
      oid: orderId
    },
    success: function (res) {
      console.log(res)
      if (res.data.Code == 0) {
        //发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          success: function (res) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            })
            wx.redirectTo({
              url: redirectUrl,
            })
            return;
          },
          fail: function (aaa) {
            wx.showModal({
              title: '提示',
              content: aaa.errMsg,
              showCancel: false
            })
            wx.redirectTo({
              url: redirectUrl,
            })
            return;
          }
        })
      }
      else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false
        })
        return;
      }
    }
  })
}

module.exports = {
  wxpay: wxpay,
  wxcashpay: wxcashpay,
  wxdirectpay: wxdirectpay
}
